// 参数加密算法,参数传递过程中，值只能为字符串或数组，不能出现对象
// 例: 不能出现包含对象的值: {"name": 2, ...}、[{"name": 2, ...}]
// 数组有自带的toString()方法，可进行字符串的加操作，如要传递对象，请进行json转码

function appendMD5 (params, type) {
  if (params === undefined) {
    return undefined;
  }
  
  var str = '', count = 0
  for (let i in params) {
    if(i != 'token'){
      str += i.substr(0, 1) + params[i];
      count++;
    }
  }
  str += count;
  if (str !== '') {
    if (type.toLowerCase() != 'post') {
      str = encodeURIComponent(str);
    }
    str = hex_md5(str);
    count = count % 10;
    var mac = str.substr(0, count) + count + str.substr(count);
    return {
      ...params,
      mac: mac.toUpperCase()
    }
  }
}

var website = 'http://localhost/restfulapi/public/index.php';
var jiami = {'id': 'HeroAcv', 'userName': 'hahahah'};

$.ajax({
  type: "GET",
  url: website + "/user",
  data: appendMD5(jiami, 'get'),
  dataType: "json",
  success: function(data){
    console.log(JSON.parse(data));
  }
});

/*$.ajax({
  type: "post",
  url: website + "/get_token",
  data: appendMD5(jiami, 'post'),
  dataType: "json",
  success: function(data){
    localStorage.setItem('token', JSON.parse(data).result.token);
  }
});*/

/* var apa = {"token": localStorage.getItem('token')};
$.ajax({
  type: "post",
  url: website + "/test_token",
  data: appendMD5(apa, 'post'),
  dataType: "json",
  success: function(data){
    console.log(JSON.parse(data));
  }
}); */